package com.xxx.blog.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xxx.base.utils.R;
import com.xxx.blog.domain.ArticleType;
import com.xxx.blog.query.ArticleTypeQuery;
import com.xxx.blog.service.IArticleTypeService;
import com.xxx.vo.ArticleTypeStatistics;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/articleType")
@Api(tags = "文章类型接口类")
public class ArticleTypeController {

    @Autowired
    private IArticleTypeService articleTypeService;

    @GetMapping
    @ApiOperation(value = "查询所有文章类型")
    public List<ArticleType> findAll() {
        return articleTypeService.findAll();
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询文章类型")
    public R<ArticleType> findOne(@PathVariable long id) {
        ArticleType articleType = articleTypeService.findOne(id);
        return R.ok(articleType);
    }

    @PostMapping
    @ApiOperation(value = "分页加条件查询")
    public R<PageInfo<ArticleType>> queryPage(@RequestBody ArticleTypeQuery articleTypeQuery) {
        List<ArticleType> list = articleTypeService.queryPage(articleTypeQuery);
        return R.ok(new PageInfo<ArticleType>(list));
    }

    @PutMapping
    @ApiOperation(value = "增加或者修改")
    public R<Void> updateAndAddArticleType(@RequestBody ArticleType articleType) {
        try {
            Long id = articleType.getId();
            if (id == null) {
                articleTypeService.addArticleType(articleType);
            } else {
                articleTypeService.updateArticleType(articleType);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail();
        }
        return R.ok();
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "通过id删除文章类型")
    public R<Void> deleteArticleType(@PathVariable long id) {
        try {
            articleTypeService.deleteArticleType(id);
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail();
        }
        return R.ok();
    }

    @PatchMapping
    @ApiOperation(value = "批量删除")
    public R<Void> batchArticleTypes(@RequestBody long[] ids) {
        try {
            articleTypeService.batchArticleTypes(ids);
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail();
        }
        return R.ok();
    }


    @GetMapping("/typeTree/{id}")
    @ApiOperation(value = "查询类型树")
    public R<List<ArticleType>> getTypeTree( @PathVariable Long id) {
        List<ArticleType> typeTree = articleTypeService.getTypeTree(id);
        return R.ok(typeTree);
    }


    @GetMapping("/articleTypeStatistics")
    @ApiOperation(value = "文章类型统计")
    public R<List<ArticleTypeStatistics>> findArticleTypeStatistics() {
        return  R.ok(articleTypeService.findArticleTypeStatistics());

    }
}
